package com.ww.springboot.sharding.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ww.springboot.sharding.entity.OutOrderDetailsEntity;
import com.ww.springboot.sharding.mapper.OutOrderDetailsMapper;
import com.ww.springboot.sharding.service.IOutOrderDetailsService;
import com.ww.springboot.sharding.vo.FindOrderVO;
import com.ww.springboot.sharding.vo.OutOrderDetailsVO;
import com.ww.springboot.sharding.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class OutOrderDetailsServiceImpl extends ServiceImpl<OutOrderDetailsMapper, OutOrderDetailsEntity> implements IOutOrderDetailsService {

    @Resource
    private OutOrderDetailsMapper outOrderDetailsMapper;


    /**
     * /**
     * 分页查询订单详情(订单-物流商-发货方式-轨迹表)
     */
    @Override
    public List<OutOrderDetailsVO> selectOutOrderDetailsPage(FindOrderVO findOrderVO, PageVO page) {
        return outOrderDetailsMapper.selectOutOrderDetailsPage(findOrderVO, page);
    }


    @Override
    public int outOrderDetailsSize(FindOrderVO findOrderVO) {
        return outOrderDetailsMapper.outOrderDetailsSize(findOrderVO);
    }

    @Override
    public void outOrderDetailsUpdate(String oId) {
        outOrderDetailsMapper.outOrderDetailsUpdate(oId);
    }


    @Override
    public void insertOutOrderDetails(List<OutOrderDetailsEntity> list) {
        outOrderDetailsMapper.insertOutOrderDetails(list);
    }

    @Override
    public void updateOutOrderDetails(List<OutOrderDetailsEntity> list) {
        outOrderDetailsMapper.updateOutOrderDetails(list);
    }

    @Override
    public void updateOutOrderDetailsObj(OutOrderDetailsEntity obj) {
        outOrderDetailsMapper.updateOutOrderDetailsObj(obj);
    }

    @Override
    public void updateTraceStatus(OutOrderDetailsEntity outOrderDetailsEntity) {
        UpdateWrapper<OutOrderDetailsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", outOrderDetailsEntity.getId());
        updateWrapper.eq("o_outstock_time", outOrderDetailsEntity.getOutstockTime());
        OutOrderDetailsEntity updateEntity = new OutOrderDetailsEntity();
        updateEntity.setTrajectoryType(outOrderDetailsEntity.getTrajectoryType());
        updateEntity.setDelayType(outOrderDetailsEntity.getDelayType());
        updateEntity.setSignDay(outOrderDetailsEntity.getSignDay());
        super.update(updateEntity, updateWrapper);
    }

    @Override
    public List<OutOrderDetailsEntity> findOrders(Date startTime, Date endTime, Integer pageStart, Integer pageSize, List<Integer> traceStatus) {
        return outOrderDetailsMapper.findOrders(startTime, endTime, pageStart, pageSize, traceStatus);
    }

    @Override
    public List<OutOrderDetailsEntity> findOrdersById(Long diId, Long dgId, Date startTime, Date endTime, Integer pageStart, Integer pageSize, List<Integer> traceStatus) {
        return outOrderDetailsMapper.findOrderById(diId, dgId, startTime, endTime, pageStart, pageSize, traceStatus);
    }

    @Override
    public OutOrderDetailsEntity findOrderByTrackingNumber(String trackingNumber, Collection<Date> dates) {
        if (StrUtil.isEmpty(trackingNumber)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("o_tracknumber", trackingNumber);
        if (CollUtil.isNotEmpty(dates)) {
            queryWrapper.in("o_outstock_time", dates);
        }
        List<OutOrderDetailsEntity> list = super.list(queryWrapper);
        return CollUtil.isEmpty(list) ? null : list.get(0);
    }


    @Override
    public OutOrderDetailsEntity findOrderByTrackingNumberOrEx(String trackingNumber, Collection<Date> dates) {
        if (StrUtil.isEmpty(trackingNumber)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("o_tracknumber_ex", trackingNumber);
        queryWrapper.or().eq("o_tracknumber", trackingNumber);
        if (CollUtil.isNotEmpty(dates)) {
            queryWrapper.in("o_outstock_time", dates);
        }
        List<OutOrderDetailsEntity> list = super.list(queryWrapper);
        return CollUtil.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public OutOrderDetailsEntity findOrderByoId(Long oId, Collection<Date> dates) {
        if (ObjectUtil.isEmpty(oId)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("o_id", oId);
        if (CollUtil.isNotEmpty(dates)) {
            queryWrapper.in("o_outstock_time", dates);
        }
        List<OutOrderDetailsEntity> list = super.list(queryWrapper);
        return CollUtil.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List<OutOrderDetailsEntity> findOrderByoId(Collection<Object> oId, Collection<Date> dates) {
        if (CollUtil.isEmpty(oId)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("o_id", oId);
        if (CollUtil.isNotEmpty(dates)) {
            List<Date> sort = CollUtil.sort(dates, Date::compareTo);
            queryWrapper.ge("o_outstock_time", sort.get(0));
            queryWrapper.le("o_outstock_time", sort.get(sort.size() - 1));
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<OutOrderDetailsEntity> findOrderByTrackingList(Collection<String> trackingNumber, Collection<Date> dates) {
        if (CollUtil.isEmpty(trackingNumber)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("o_tracknumber", trackingNumber);
        if (CollUtil.isNotEmpty(dates)) {
            List<Date> sort = CollUtil.sort(dates, Date::compareTo);
            queryWrapper.ge("o_outstock_time", sort.get(0));
            queryWrapper.le("o_outstock_time", sort.get(sort.size() - 1));
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<OutOrderDetailsEntity> findOrderByTrackingExList(Collection<String> trackingNumberEx, Collection<Date> dates) {
        if (CollUtil.isEmpty(trackingNumberEx)) {
            return null;
        }
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("o_tracknumber_ex", trackingNumberEx);
        if (CollUtil.isNotEmpty(dates)) {
            List<Date> sort = CollUtil.sort(dates, Date::compareTo);
            queryWrapper.ge("o_outstock_time", sort.get(0));
            queryWrapper.le("o_outstock_time", sort.get(sort.size() - 1));
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<OutOrderDetailsEntity> findByIds(Collection<String> ids, Collection<Date> dates) {
        QueryWrapper<OutOrderDetailsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        if (CollUtil.isNotEmpty(dates)) {
            List<Date> sort = CollUtil.sort(dates, Date::compareTo);
            queryWrapper.ge("o_outstock_time", sort.get(0));
            queryWrapper.le("o_outstock_time", sort.get(sort.size() - 1));
        }
        return super.list(queryWrapper);
    }

}
